package com.example.service.aboutProvince.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.vo.request.AdmissionScoreEditForm;
import com.example.vo.request.ProvinceConditionsDTO;
import com.example.vo.request.ProvinceSumTemp;
import com.example.vo.response.CollegeMajorProvinceDTO;
import com.example.mapper.aboutProvince.CollegeMajorProvinceMapper;
import com.example.mapper.aboutStudent.StudentMapper;
import com.example.pojo.aboutProvince.CollegeMajorProvince;
import com.example.service.aboutProvince.CollegeMajorProvinceService;
import com.example.vo.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service
public class CollegeMajorProvinceServiceImpl extends ServiceImpl<CollegeMajorProvinceMapper, CollegeMajorProvince> implements CollegeMajorProvinceService {

    @Resource
    private StudentMapper studentMapper;

    /**
     *
     * @param provinceConditionsDTO 查询条件
     * @return 专业省份招生数据
     */
    @Override
    public Result getProvinceEnrollment(ProvinceConditionsDTO provinceConditionsDTO) {
        String college = provinceConditionsDTO.getCollege();
        String major = provinceConditionsDTO.getMajor();
        String grade = provinceConditionsDTO.getGrade();

        ArrayList<CollegeMajorProvinceDTO> results = new ArrayList<>();

        // 计算每个省份的 学院（全专业）平均分，最高分，最低分
        List<ProvinceSumTemp> collegePoints = studentMapper.getCollegePoints(college, grade);

        // 获取指定专业的学生，指定专业的平均分，最高分，最低分
        List<ProvinceSumTemp> majorPoints = studentMapper.getMajorPoints(college, major, grade);

        for (ProvinceSumTemp majorPoint : majorPoints) {
            // 封装一条待返回的数据
            CollegeMajorProvinceDTO result = new CollegeMajorProvinceDTO();

            String province = majorPoint.getOriginArea();

            // 根据省份、年份查询 某年某省 本科一批分数线
            LambdaQueryWrapper<CollegeMajorProvince> collegeMajorProvinceLambdaQueryWrapper = new LambdaQueryWrapper<>();
            collegeMajorProvinceLambdaQueryWrapper
                    .eq(CollegeMajorProvince::getProvince, province)
                    .eq(CollegeMajorProvince::getGrade, grade);

            CollegeMajorProvince collegeMajorProvince = super.getOne(collegeMajorProvinceLambdaQueryWrapper);

            // 表中没有 某年某省 的数据
            if(collegeMajorProvince == null) {
                // 创建 某年某省 的数据
                CollegeMajorProvince temp = new CollegeMajorProvince();
                temp.setCollege(college);
                temp.setProvince(province);
                temp.setGrade(grade);

                super.save(temp);
                collegeMajorProvince = super.getOne(collegeMajorProvinceLambdaQueryWrapper);
            }

            // 设置本科一批分数线
            result.setId(collegeMajorProvince.getId());
            result.setAdmissionScore(collegeMajorProvince.getAdmissionScore());

            // 设置其他内容
            result.setProvince(province);

            // 设置学院分数（平均、最高、最低）
            for (ProvinceSumTemp collegePoint : collegePoints) {
                if(collegePoint.getOriginArea().equals(province)) {
                    result.setAverageScoreByCollege((int) Math.round(collegePoint.getAvgPoints()));
                    result.setHighestScoreByCollege((int) Math.round(collegePoint.getMaxPoints()));
                    result.setLowestScoreByCollege((int) Math.round(collegePoint.getMinPoints()));
                }
            }

            // 设置专业分数（平均、最高、最低）
            result.setAverageScoreByMajor((int) Math.round(majorPoint.getAvgPoints()));
            result.setHighestScoreByMajor((int) Math.round(majorPoint.getMaxPoints()));
            result.setLowestScoreByMajor((int) Math.round(majorPoint.getMinPoints()));

            // 设置学生数量
            result.setStudentNum(majorPoint.getSum());

            // 设置附加信息
            result.setMajor(major);
            result.setCollege(college);
            result.setGrade(grade);

            results.add(result);
        }

        return Result.success(results);
    }

    /**
     * 更新本科一批分数线
     *
     * @param admissionScoreEditForm 包括id,分数
     * @return 是否更新成功
     */
    @Override
    public Result updateProvinceAdmissionScore(AdmissionScoreEditForm admissionScoreEditForm) {

        LambdaUpdateWrapper<CollegeMajorProvince> collegeMajorProvinceLambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        collegeMajorProvinceLambdaUpdateWrapper.eq(CollegeMajorProvince::getId, admissionScoreEditForm.getId())
                .set(CollegeMajorProvince::getAdmissionScore, admissionScoreEditForm.getAdmissionScore());

        super.update(collegeMajorProvinceLambdaUpdateWrapper);

        return Result.success();
    }

    /**
     * 批量修改省份一本分数线
     *
     * @param admissionScoreEdits 要修改的信息
     * @return 是否修改成功
     */
    @Override
    public Result editAdmissionScoreBatch(List<AdmissionScoreEditForm> admissionScoreEdits) {

        for (AdmissionScoreEditForm admissionScoreEdit : admissionScoreEdits) {
            this.updateProvinceAdmissionScore(admissionScoreEdit);
        }

        return Result.success();
    }
}
